const { db, sqlExec } = require('../db/mysql57');

const bookDao = {
    async getCommendBook(uid, lastId) {
        let sql = `select
                id,
                name,
                text,
                bookphotoId as imgId,
                (
                    select count(*) from Library where bookId = Book.id and uid = ?
                ) as isJoin
            from Book
            LIMIT 10`;
        let data = [uid];
        if (lastId) {
            sql = `select
                id,
                name,
                text,
                bookphotoId as imgId,
                (
                    select count(*) from Library where bookId = Book.id and uid = ?
                ) as isJoin
            from Book
            where id > ?
            LIMIT 10`;
            data.push(lastId);
        }
        return sqlExec(sql, data);
    },
    /*
     * 加入书架接口
     */
    async joinInLib(uid, bookId) {
        let sql = `insert into Library (uid, bookId) (
            select ?, ? where (
                select count(*) from Library where uid = ? and bookId = ?
            ) = 0
        )`;
        let data = [ uid, bookId, uid, bookId ];
        return sqlExec(sql, data);
    },
    /*
     * 取消加入书架接口
     */
    async noJoinInLib(uid, bookId) {
        let sql = `delete from Library where uid = ? and bookId = ?`;
        let data = [ uid, bookId ];
        return sqlExec(sql, data);
    },
    /*
     * 懒加载获取加入书架的书籍接口
     */
    async getJoinBook(uid, lastId) {
        let sql = `select
                b.id as id,
                l.id as lid,
                b.name as name,
                b.text as text,
                b.bookphotoId as imgId,
                1 as isJoin
            from Library as l
            left join Book as b on l.bookId = b.id
            where l.uid = ?
            LIMIT 10`;
        let data = [uid];
        if (lastId) {
            sql = `select
                b.id as id,
                l.id as lid,
                b.name as name,
                b.text as text,
                b.bookphotoId as imgId,
                1 as isJoin
            from Library as l
            left join Book as b on l.bookId = b.id
            where l.uid = ? and l.id > ?
            LIMIT 10`;
            data.push(lastId);
        }
        return sqlExec(sql, data);
    },
    async getBookContent(bookId) {
        let sql = `select id, pageNum from Book where id = ?`;
        let data = [bookId];
        return sqlExec(sql, data);
    },
    async getAllBook(page) {
        let sql = `select
                b.id as id,
                b.name as name,
                b.text as text
            from Book as b
            LIMIT ?, 50`;
        let data = [ (page-1) * 50 ];
        return sqlExec(sql, data);
    },
    async searchBook(uid, bookName) {
        let sql = `select
                id,
                name,
                text,
                bookphotoId as imgId,
                (
                    select count(*) from Library where bookId = Book.id and uid = ?
                ) as isJoin
            from Book
            where name like ?
            LIMIT 10`;
        let data = [ uid, `%${bookName}%` ];
        return sqlExec(sql, data);
    }
};

module.exports = bookDao;